/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginFrame.java
 *
 * Created on Jan 3, 2012, 10:53:28 PM
 */
package makao;

import java.awt.Color;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.util.regex.Pattern.*;

public class LoginFrame extends javax.swing.JFrame
{

    private MakaoView makaoView = null;
    private Pattern pattern;
    private Matcher matcher;
    private static final String IPADDRESS_PATTERN =
            "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
            + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

    public LoginFrame()
    {
        initComponents();
        pattern = Pattern.compile(IPADDRESS_PATTERN);
//        try
//        {
//            initComponents();
//            MaskFormatter formatter = new MaskFormatter("###.###.###.###");
//            DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
//            jFormattedTextFieldAddress.setFormatterFactory(factory);
//            jFormattedTextFieldAddress.setValue("000.000.000.000");
//            
//            
//        } catch (ParseException ex)
//        {
//            Logger.getLogger(LoginFrame.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        
    }

    public void setMakaoView(MakaoView makaoView)
    {
        this.makaoView = makaoView;
    }

    public void setFocus()
    {
        jTextFieldIpAddress.grabFocus();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldLogin = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jButtonConnect = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jTextFieldIpAddress = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(makao.MakaoApp.class).getContext().getResourceMap(LoginFrame.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAlwaysOnTop(true);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 20, -1, 30));

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 60, -1, 30));

        jTextFieldLogin.setText(resourceMap.getString("jTextFieldLogin.text")); // NOI18N
        jTextFieldLogin.setName("jTextFieldLogin"); // NOI18N
        getContentPane().add(jTextFieldLogin, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 60, 190, 30));

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jButtonConnect.setText(resourceMap.getString("jButtonConnect.text")); // NOI18N
        jButtonConnect.setName("jButtonConnect"); // NOI18N
        jButtonConnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConnectActionPerformed(evt);
            }
        });

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setPreferredSize(new java.awt.Dimension(80, 25));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextFieldIpAddress.setText(resourceMap.getString("jTextFieldIpAddress.text")); // NOI18N
        jTextFieldIpAddress.setName("jTextFieldIpAddress"); // NOI18N
        jTextFieldIpAddress.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldIpAddressFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldIpAddressFocusLost(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(119, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonConnect, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jTextFieldIpAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(81, 81, 81))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jTextFieldIpAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonConnect)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 390, 150));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonConnectActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonConnectActionPerformed
    {//GEN-HEADEREND:event_jButtonConnectActionPerformed
        if ((!jTextFieldLogin.getText().isEmpty()) && (!jTextFieldIpAddress.getText().isEmpty()) && (makaoView != null))
        {
            matcher = pattern.matcher(jTextFieldIpAddress.getText());
            if (matcher.matches())
            {
                this.setVisible(false);
                makaoView.startUpConnection(String.valueOf(jTextFieldIpAddress.getText()), jTextFieldLogin.getText());
            } else
            {
                jTextFieldIpAddress.setForeground(Color.red);
                jTextFieldIpAddress.setText(jTextFieldIpAddress.getText());
            }
        }
    }//GEN-LAST:event_jButtonConnectActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        this.setVisible(false);
        makaoView.startUpConnection(null, null);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextFieldIpAddressFocusGained(java.awt.event.FocusEvent evt)//GEN-FIRST:event_jTextFieldIpAddressFocusGained
    {//GEN-HEADEREND:event_jTextFieldIpAddressFocusGained
        jTextFieldIpAddress.setForeground(Color.black);
        jTextFieldIpAddress.setText(jTextFieldIpAddress.getText());
    }//GEN-LAST:event_jTextFieldIpAddressFocusGained

    private void jTextFieldIpAddressFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_jTextFieldIpAddressFocusLost
    {//GEN-HEADEREND:event_jTextFieldIpAddressFocusLost
        matcher = pattern.matcher(jTextFieldIpAddress.getText());
        if (!matcher.matches())
        {
            jTextFieldIpAddress.setForeground(Color.red);
            jTextFieldIpAddress.setText(jTextFieldIpAddress.getText());
        }
    }//GEN-LAST:event_jTextFieldIpAddressFocusLost

    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            public void run()
            {
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonConnect;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextFieldIpAddress;
    private javax.swing.JTextField jTextFieldLogin;
    // End of variables declaration//GEN-END:variables
}
